package com.cntcdt.api.db.dao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.cntcdt.api.db.dao.base.ConsociationCompanyDaySumDaoBase;
import com.cntcdt.api.db.model.ConsociationCompanyDayCntModel;
import com.cntcdt.api.db.model.ConsociationCompanyDaySumModel;
import com.cntcdt.api.db.model.ConsociationCompanyMonthSumModel;

/**
 * 这是cdt_consociation_company_day_sum的Dao实现类
 */
public class ConsociationCompanyDaySumDao extends ConsociationCompanyDaySumDaoBase   {
    /**
     * ConsociationCompanyDaySumDao对象
     */
    private static ConsociationCompanyDaySumDao instance = new ConsociationCompanyDaySumDao();

    /**
     * 默认构造方法
     */
    private ConsociationCompanyDaySumDao() {
    }

    /**
     * 获得对象实例
     */
    public static ConsociationCompanyDaySumDao getInstance() { 
         return instance;
    } 
    
    /**
     * 按昨天的集团数据
     * @param id id
     * @return model
     */
    public List<ConsociationCompanyDaySumModel> findByYesterday(String consociationCompanyId) {
		Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);            
        String  yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        String sql = "SELECT * FROM " + TABLE + " WHERE addTime = ? and consociationCompanyId in ("+consociationCompanyId+") order by addTime";
       Object[] paramObjs = {yestedayDate};
       List<ConsociationCompanyDaySumModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   }  
    
    /**
     * 查出本月的集团数据
     * @param id id
     * @return model
     */
    public List<ConsociationCompanyDaySumModel> findByThisMonth(String consociationCompanyId) {
        Calendar cal_2 = Calendar.getInstance();    
        cal_2.add(Calendar.MONTH, 0);
        cal_2.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal_2.getTime());
    	
		Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 0);            
        String  todayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        System.out.println(firstDay);
        System.out.println(todayDate);
        String sql = "SELECT * FROM " + TABLE + " WHERE addTime >= ? and addTime <= ? and consociationCompanyId in ("+consociationCompanyId+") order by addTime";
       Object[] paramObjs = {firstDay,todayDate};
       List<ConsociationCompanyDaySumModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   } 
    
    /**
     * 查出近7天的集团数据
     * @param id id
     * @return model
     */
    public List<ConsociationCompanyDaySumModel> findByLastSevenDayData(String consociationCompanyId) {
		Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);            
        String  yesterdayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, -7);            
        String  lastSevenDayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar2.getTime());
        
        String sql = "SELECT id,SUM(borrowUserSum) as borrowUserSum,SUM(borrowCDBSum) as borrowCDBSum,SUM(newUserSum) as newUserSum,SUM(borrowRentSum) as borrowRentSum,SUM(sellLineMoneySum) as sellLineMoneySum,addTime,updateTime,consociationCompanyId,sortTime,SUM(sellCDBSum) as sellCDBSum,SUM(sellLineNum) as sellLineNum FROM " + TABLE + " WHERE addTime >= ? and addTime <= ? and consociationCompanyId in ("+consociationCompanyId+") GROUP BY addTime";
       Object[] paramObjs = {lastSevenDayDate,yesterdayDate};
       List<ConsociationCompanyDaySumModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   } 
    
    /**
     * 分页查询
     * @param id id
     * @return model
     */
    public List<ConsociationCompanyDaySumModel> findBySortTime(long sortTime,String consociationCompanyId) {
        String sql = "SELECT id,SUM(borrowUserSum) as borrowUserSum,SUM(borrowCDBSum) as borrowCDBSum,SUM(newUserSum) as newUserSum,SUM(borrowRentSum) as borrowRentSum,SUM(sellLineMoneySum) as sellLineMoneySum,addTime,updateTime,consociationCompanyId,sortTime,SUM(sellCDBSum) as sellCDBSum,SUM(sellLineNum) as sellLineNum FROM " + TABLE + " WHERE consociationCompanyId in ("+consociationCompanyId+") and sortTime < ? GROUP BY addTime order by sortTime desc limit 20";
       Object[] paramObjs = {sortTime};
       List<ConsociationCompanyDaySumModel> modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   }
    
    /**
     * 根据天数查询数据
     * @param id id
     * @return model
     */
    public List<ConsociationCompanyDaySumModel> findByDayNum(Integer dayNum,String terminal) {
		Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);            
        String  yesterdayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, -dayNum);            
        String  lastSevenDayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar2.getTime());
        
        String sql = "SELECT * FROM " + TABLE + " WHERE addTime >= ? and addTime <= ?  and terminal = ? order by addTime";
       Object[] paramObjs = {lastSevenDayDate,yesterdayDate,terminal};
       List<ConsociationCompanyDaySumModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   }

}
